﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:04
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  UserInRoleRepository : Repository<UserInRole, UserInRole, TbUserInRole>, IUserInRoleRepository<UserInRole>
    {
        public override UserInRole CreateEntity()
        {
            return new UserInRole { };
        }
        public override IQueryable<UserInRole> Get()
        {
            return MapQueryable(Entities.TbUserInRole);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbUserInRole).Count();
        }
        public override IQueryable<UserInRole> Get(Expression<Func<UserInRole, bool>> expression)
        {
            return MapQueryable(Entities.TbUserInRole).Where(expression);
        }
        public override IQueryable<UserInRole> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbUserInRole), sort, order);
        }
        public override IQueryable<UserInRole> GetSort(Expression<Func<UserInRole, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbUserInRole).Where(expression), sort, order);
        }
        public override void Add(UserInRole t)
        {
            Entities.AddToTbUserInRole(ReverseMapModel(t));
        }
        public override void Update(UserInRole t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(UserInRole t)
        {
            if (Entities.TbUserInRole.Any(o => o.CoUserInRole == t.Id))
            {

            }
            else
            {
                Entities.AddToTbUserInRole(ReverseMapModel(t));
            }
        }
        public override void Delete(UserInRole t)
        {
            Entities.TbUserInRole.Where(o => o.CoUserInRole == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<UserInRole, bool>> expression)
        {
            MapQueryable(Entities.TbUserInRole).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<UserInRole> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<UserInRole>();
        }
      
        public override sealed IQueryable<UserInRole> MapQueryable(IQueryable<TbUserInRole> queryable)
        {
            return queryable.Select(o => new UserInRole 
            {
         
	           Id=o.CoUserInRole,
	           RoleId = o.CoRoleId 
,
	           UserId = o.CoUserId 
            
            });
        }
      
        public override sealed IQueryable<TbUserInRole> ReverseMapQueryable(IQueryable<UserInRole> queryable)
        {
            return queryable.Select(o => new TbUserInRole 
            { 
        
	           CoUserInRole=o.Id,
	           CoRoleId = o.RoleId
,
	           CoUserId = o.UserId
  
            
            });
        }
      
        public override sealed UserInRole MapModel(TbUserInRole model)
        {
            return new UserInRole {
	           Id=model.CoUserInRole,
	           RoleId = model.CoRoleId
,
	           UserId = model.CoUserId
            };
        }
        
        public override sealed TbUserInRole ReverseMapModel(UserInRole model)
        {
            return new TbUserInRole { 
	           CoUserInRole=model.Id,
	           CoRoleId = model.RoleId
	        
,
	           CoUserId = model.UserId
	        
            
            
            };
        }


    }
}



